package classes.Windows;

import db.Jdbc;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @Auther: 程嘉明
 * @Date: 2018/8/30/030 18:58
 * @Description:
 */
public class WinManager extends Window {

    public WinManager(int length, int width, String title, String ID, String NAME) throws Exception {
        super(length, width, title);
        JLabel lab1 = new JLabel("经理管理界面");
        lab1.setFont(new Font("宋体", 1, 18));
        lab1.setBounds(0, 20, 800, 60);
        lab1.setBackground(Color.blue);
        lab1.setOpaque(true);
        this.addP(lab1);
        JLabel lab2 = new JLabel("欢迎：");
        lab2.setBounds(0, 60, 120, 80);
        lab2.setFont(new Font("宋体", 1, 14));
        this.addP(lab2);
        Jdbc.getConnection();
        ResultSet rs = Jdbc.stat.executeQuery("select NAME from t_gdryjbxx where ID='" + ID + "' ");
        rs.next();
        JLabel lab3 = new JLabel(rs.getString("NAME"));
        lab3.setFont(new Font("宋体", 1, 14));
        lab3.setBounds(70, 60, 120, 80);
        this.addP(lab3);
        JLabel lab4 = new JLabel("查看上班打卡的工人");
        lab4.setFont(new Font("宋体", 1, 14));
        lab4.setBounds(0, 120, 180, 80);
        this.addP(lab4);
        JButton jb1 = new JButton("查询");
        jb1.setBounds(600, 140, 180, 40);
        this.addP(jb1);
        String[] title1 = {"工人ID", "时间", "上午打卡次数", "下午打卡次数", "姓名"};
        Object[][] rows = new Object[][]{{"待查询", "待查询", "待查询", "待查询", "待查询"}};
        DefaultTableModel model = new DefaultTableModel(rows, title1);
        JTable tab = new JTable(model);
        JScrollPane jp = new JScrollPane(tab);//滚动版面
        tab.setBounds(0, 0, 500, 200);
        jp.setBounds(0, 200, 500, 200);
        tab.setPreferredScrollableViewportSize(new Dimension(550, 100));
        this.addP(jp);
        AtomicBoolean click = new AtomicBoolean(false);//是否点击
        jb1.addActionListener(e -> {
            if (!click.get()) {
                model.removeRow(0);
                try {
                    Jdbc.getConnection();
                    ResultSet rs1 = Jdbc.stat1.executeQuery("select * from clockin ");
                    while (rs1.next()) {
                        ArrayList<String> row = new ArrayList<>();
                        row.add(rs1.getString("ID"));
                        row.add(rs1.getDate("time").toString());
                        row.add(new Integer(rs1.getInt("M")).toString());
                        row.add(new Integer(rs1.getInt("E")).toString());
                        row.add(rs1.getString("NAME"));
                        model.insertRow(0, row.toArray());
                    }
                    jb1.setText("刷新");
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
                click.set(true);
            } else {
                JOptionPane.showMessageDialog(null, "刷新成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
            }
        });
        JLabel lab5 = new JLabel("查看工资");
        lab5.setBounds(0, 380, 120, 80);
        lab5.setFont(new Font("宋体", 1, 14));
        this.addP(lab5);
        JLabel lab6 = new JLabel("输入查询的ID：");
        lab6.setFont(new Font("宋体", 1, 14));
        lab6.setBounds(120, 380, 120, 80);
        this.addP(lab6);
        JTextField jt1 = new JTextField("T000001");
        jt1.setBounds(220, 405, 180, 30);
        this.addP(jt1);
        JButton jb2 = new JButton("查询");
        jb2.setBounds(600, 400, 180, 40);
        this.addP(jb2);
        String[] title2 = {"工人ID", "姓名", "基本工资", "提成", "总金额"};
        Object[][] rows1 = new Object[][]{{"待查询", "待查询", "待查询", "待查询", "待查询"}};
        DefaultTableModel model1 = new DefaultTableModel(rows1, title2);
        JTable tab1 = new JTable(model1);
        JScrollPane jp1 = new JScrollPane(tab1);//滚动版面
        tab1.setBounds(0, 0, 500, 45);
        jp1.setBounds(0, 440, 500, 45);
        tab1.setPreferredScrollableViewportSize(new Dimension(550, 100));
        this.addP(jp1);
        AtomicBoolean click1 = new AtomicBoolean(false);//是否点击
        jb2.addActionListener(e -> {
            if (!click1.get()) {
                model1.removeRow(0);
                String S_ID = jt1.getText();
                try {
                    ResultSet rs2 = Jdbc.stat.executeQuery("select * from salary where ID='" + S_ID + "'");
                    ResultSet rs3 = Jdbc.stat2.executeQuery("select NAME from t_gdryjbxx where ID='" + S_ID + "'");
                    rs3.next();
                    String S_NAME = rs3.getString("NAME");
                    rs2.next();
                    ArrayList<String> row = new ArrayList<>();
                    row.add(rs2.getString("ID"));
                    row.add(S_NAME);
                    row.add(rs2.getString("base"));
                    row.add(rs2.getString("commission"));
                    row.add(rs2.getString("net payroll"));
                    model1.insertRow(0, row.toArray());
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            } else {
                JOptionPane.showMessageDialog(null, "刷新成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
            }
        });
    }

    public static void main(String[] args) throws Exception {
        new WinManager(800, 600, "经理管理界面", "T000001", "李武");
    }
}
